﻿<template>
    <elm-page-bb v-loading="loading">
        <elm-table-tree ref="tree"  @select="handleSelectionChange" url="/system/menu/GetTree" :default-expand-all="true" @change="getData" :page-size="100" height="100%">
            <el-table-column type="selection" width="55"   v-if="request.id!='1'"></el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
<!--            <el-table-column prop="navigateurl" label="路径"></el-table-column>-->
            <el-table-column prop="remark" label="说明"></el-table-column>
        </elm-table-tree>
        <template #bottom>
            <el-button type="success" size="small" @click="submit"  v-if="request.id!='1'">立即保存</el-button>
            <el-button size="small" @click="close">取消</el-button>
        </template>
    </elm-page-bb>
</template>

<script>
    export default {
        extends: elmVueBase,
        data() {
            return {
            }
        },
        created() {
        },
        mounted() {
        },
        methods: {
          handleSelectionChange(selection, row) {
            this.$nextTick(()=>{
              var c = this.$refs.tree.$children[0].selection.some(m=> m.id == row.id);
              if (row.children && row.children.length > 0) {
                row.children.forEach((item) => {
                  this.$refs.tree.$children[0].toggleRowSelection(item,c);
                });
              }
            })
          },
            async getData() {
                this.loading = true;
                var keys = await elm.post('/system/role/GetRoleMenu', { role_id: this.request.id });
                  elm.treeRec(this.$refs.tree.datalist, (child) => {
                    if (keys.some(item=>item.id==child.id)) {
                      this.$refs.tree.$children[0].toggleRowSelection(child, true);
                    }
                  })
                this.loading = false;
            },
            async submit() {
                var items = [];
                for (var select of this.$refs.tree.$children[0].selection) {
                  items.push({menu_id:select.id, role_id: this.request.id});
                }
                let res = await elm.post('/system/role/SaveRoleMenu', { role_id: this.request.id, items: items });
                if (res>0) {
                    this.close();
                    this.v('保存成功。');
                    this.request.callVue.tablebinding();
                    var resm = await this.post('/system/login/GetUserMenu');
                    elm.main.vue.menuData=elm.info.menu = resm;
                }
                else {
                    this.error('保存失败，请稍后尝试。');
                }

            }
        }
    }
</script>

<style scoped>
</style>